<template>
  <div class="app-container">
    <div class="search-area" v-show="showSearch">
        <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" class="clearfix" label-width="auto">
          <el-form-item label="游戏名称" prop="name">
            <el-input
                v-model="queryParams.name"
                placeholder="请输入游戏名称"
                clearable
                @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
    </div>
    <div class="main-content">
      <el-row :gutter="10" class="mb24">
        <el-col :span="1.5">
          <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['biz:game:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['biz:game:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['biz:game:remove']"
          >删除</el-button>
        </el-col>
                                                                                                            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="gameList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="游戏名称" align="center" prop="name" v-if="columns.name.visible" />
                <el-table-column label="游戏图标URL" align="center" prop="iconUrl" v-if="columns.iconUrl.visible" />
                <el-table-column label="游戏封面图URL" align="center" prop="coverUrl" v-if="columns.coverUrl.visible" />
                <el-table-column label="游戏描述" align="center" prop="description" v-if="columns.description.visible" />
                <el-table-column label="游戏分类，如MOBA、FPS、RPG等" align="center" prop="category" v-if="columns.category.visible" />
                <el-table-column label="是否为热门游戏：0-否 1-是" align="center" prop="isHot" v-if="columns.isHot.visible" />
                <el-table-column label="是否可用：0-下架 1-上架" align="center" prop="isAvailable" v-if="columns.isAvailable.visible" />
                <el-table-column label="排序权重，越大越靠前" align="center" prop="sortOrder" v-if="columns.sortOrder.visible" />
                <el-table-column label="平台该游戏最低陪玩价" align="center" prop="minPrice" v-if="columns.minPrice.visible" />
                <el-table-column label="平台该游戏最高陪玩价" align="center" prop="maxPrice" v-if="columns.maxPrice.visible" />
                <el-table-column label="平台该游戏平均陪玩价" align="center" prop="avgPrice" v-if="columns.avgPrice.visible" />
                <el-table-column label="提供此游戏服务的陪玩师数量" align="center" prop="playerCount" v-if="columns.playerCount.visible" />
                <el-table-column label="此游戏的总订单量" align="center" prop="orderCount" v-if="columns.orderCount.visible" />
                <el-table-column label="最后操作的管理员ID" align="center" prop="operatorId" v-if="columns.operatorId.visible" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="text"
                icon="el-icon-tickets"
                @click="handleUpdate(scope.row, true)"
                v-hasPermi="['biz:game:query']"
            >查看</el-button>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['biz:game:edit']"
            >修改</el-button>
            <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['biz:game:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
    </div>



    <!-- 添加或修改游戏对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="850px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" :disabled="disabled" :class="{'detail-disabled': disabled}">
        <el-form-item label="游戏名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入游戏名称" />
        </el-form-item>
        <el-form-item label="游戏图标URL">
          <file-upload v-model="form.iconUrl" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="游戏封面图URL">
          <file-upload v-model="form.coverUrl" :disabled="disabled"/>
        </el-form-item>
        <el-form-item label="游戏描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="游戏分类，如MOBA、FPS、RPG等" prop="category">
            <el-input v-model="form.category" placeholder="请输入游戏分类，如MOBA、FPS、RPG等" />
        </el-form-item>
        <el-form-item label="是否为热门游戏：0-否 1-是" prop="isHot">
            <el-input v-model="form.isHot" placeholder="请输入是否为热门游戏：0-否 1-是" />
        </el-form-item>
        <el-form-item label="是否可用：0-下架 1-上架" prop="isAvailable">
            <el-input v-model="form.isAvailable" placeholder="请输入是否可用：0-下架 1-上架" />
        </el-form-item>
        <el-form-item label="排序权重，越大越靠前" prop="sortOrder">
            <el-input v-model="form.sortOrder" placeholder="请输入排序权重，越大越靠前" />
        </el-form-item>
        <el-form-item label="平台该游戏最低陪玩价" prop="minPrice">
            <el-input v-model="form.minPrice" placeholder="请输入平台该游戏最低陪玩价" />
        </el-form-item>
        <el-form-item label="平台该游戏最高陪玩价" prop="maxPrice">
            <el-input v-model="form.maxPrice" placeholder="请输入平台该游戏最高陪玩价" />
        </el-form-item>
        <el-form-item label="平台该游戏平均陪玩价" prop="avgPrice">
            <el-input v-model="form.avgPrice" placeholder="请输入平台该游戏平均陪玩价" />
        </el-form-item>
        <el-form-item label="提供此游戏服务的陪玩师数量" prop="playerCount">
            <el-input v-model="form.playerCount" placeholder="请输入提供此游戏服务的陪玩师数量" />
        </el-form-item>
        <el-form-item label="此游戏的总订单量" prop="orderCount">
            <el-input v-model="form.orderCount" placeholder="请输入此游戏的总订单量" />
        </el-form-item>
        <el-form-item label="最后操作的管理员ID" prop="operatorId">
            <el-input v-model="form.operatorId" placeholder="请输入最后操作的管理员ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!disabled">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGame, getGame, delGame, addGame, updateGame } from "@/api/biz/game";

export default {
  name: "Game",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 游戏表格数据
      gameList: [],
      // 弹出层标题
      title: "",
      // 是否禁止表单填写
      disabled: false,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 列信息
      columns: {
          name: {label: "游戏名称", visible: true},
          iconUrl: {label: "游戏图标URL", visible: true},
          coverUrl: {label: "游戏封面图URL", visible: true},
          description: {label: "游戏描述", visible: true},
          category: {label: "游戏分类，如MOBA、FPS、RPG等", visible: true},
          isHot: {label: "是否为热门游戏：0-否 1-是", visible: true},
          isAvailable: {label: "是否可用：0-下架 1-上架", visible: true},
          sortOrder: {label: "排序权重，越大越靠前", visible: true},
          minPrice: {label: "平台该游戏最低陪玩价", visible: true},
          maxPrice: {label: "平台该游戏最高陪玩价", visible: true},
          avgPrice: {label: "平台该游戏平均陪玩价", visible: true},
          playerCount: {label: "提供此游戏服务的陪玩师数量", visible: true},
          orderCount: {label: "此游戏的总订单量", visible: true},
          operatorId: {label: "最后操作的管理员ID", visible: true}
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "游戏名称不能为空", trigger: "blur" }
        ],
        iconUrl: [
          { required: true, message: "游戏图标URL不能为空", trigger: "blur" }
        ],
        isHot: [
          { required: true, message: "是否为热门游戏：0-否 1-是不能为空", trigger: "blur" }
        ],
        isAvailable: [
          { required: true, message: "是否可用：0-下架 1-上架不能为空", trigger: "blur" }
        ],
        sortOrder: [
          { required: true, message: "排序权重，越大越靠前不能为空", trigger: "blur" }
        ],
        playerCount: [
          { required: true, message: "提供此游戏服务的陪玩师数量不能为空", trigger: "blur" }
        ],
        orderCount: [
          { required: true, message: "此游戏的总订单量不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询游戏列表 */
    getList() {
      this.loading = true;
      listGame(this.formatParamsBatch(this.queryParams)).then(response => {
        this.gameList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        iconUrl: null,
        coverUrl: null,
        description: null,
        category: null,
        isHot: null,
        isAvailable: null,
        sortOrder: null,
        minPrice: null,
        maxPrice: null,
        avgPrice: null,
        playerCount: null,
        orderCount: null,
        createTime: null,
        updateTime: null,
        operatorId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.disabled = false;
      this.title = "添加游戏";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row, readOnly) {
      this.reset();
      const id = row.id || this.ids
      getGame(id).then(response => {
        this.form = response.data;
        this.disabled = readOnly ? true : false;
        this.title = readOnly ? "游戏详情" : "修改游戏";
        this.open = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGame(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGame(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除游戏编号为"' + ids + '"的数据项？').then(function() {
        return delGame(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/game/export', {
        ...this.queryParams
      }, `game_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
  .el-date-editor, .el-select{
    width: 100%;
  }
</style>
